﻿using LeanApp.Bll;
using LiveChartsCore;
using LiveChartsCore.Measure;
using LiveChartsCore.SkiaSharpView;
using LiveChartsCore.SkiaSharpView.Painting;
using SkiaSharp;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Controls;

namespace LeanApp.View
{
    /// <summary>
    /// CumulativeBarCtrl.xaml 的交互逻辑
    /// </summary>
    public partial class CumulativeBarCtrl : UserControl
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="title">标题</param>
        /// <param name="bll">山积图分析业务</param>
        public CumulativeBarCtrl(string title, CumulativeBll bll)
        {
            InitializeComponent();
            _title = title;
            _bll = bll;
            InitialChart();
        }

        /// <summary>
        /// 初始化图表
        /// </summary>
        private void InitialChart()
        {
            title.Text = _title;
            List<ISeries> series = new List<ISeries>();
            List<Axis> xAxises = new List<Axis>();
            List<Axis> yAxises = new List<Axis>();
            if (_bll != null && _bll.ResultData != null && _bll.ResultData.Count() > 0)
            {
                List<List<double?>> listData = new List<List<double?>>();
                int count = _bll.ResultData.Values.Select(a => a.Count).Max();
                for (int m = 0; m < count; m++)
                {
                    List<double?> data = new List<double?>();
                    foreach (string name in _bll.ResultData.Keys)
                    {
                        if (name == "id")
                        {
                            continue;
                        }
                        List<double> vals = _bll.ResultData[name];
                        if (m >= vals.Count)
                        {
                            data.Add(null);
                        }
                        else
                        {
                            if (vals[m] == 0)
                            {
                                data.Add(null);
                            }
                            else
                            {
                                data.Add(vals[m]);
                            }
                        }
                    }
                    listData.Add(data);
                }
                int i = 0;
                foreach (var item in listData)
                {
                    StackedColumnSeries<double?> cht = new StackedColumnSeries<double?>()
                    {
                        Values = item,
                        Stroke = null,
                        DataLabelsPaint = new SolidColorPaint(ColorList[i++]),
                        DataLabelsSize = 14,
                        DataLabelsPosition = DataLabelsPosition.Middle

                    };
                    if (i == ColorList.Count)
                    {
                        i = 0;
                    }
                    series.Add(cht);
                }


                Axis xAxis = new Axis()
                {
                    Labels = _bll.GetColMapper().FindAll(a => a != "id").ToArray()
                };
                xAxises.Add(xAxis);
            }


            chart.Series = series;
            chart.XAxes = xAxises;
        }

        /// <summary>
        /// 数据实体
        /// </summary>
        private CumulativeBll _bll = new CumulativeBll();
        /// <summary>
        /// 标题
        /// </summary>
        private string _title;

        /// <summary>
        /// 颜色列表
        /// </summary>
        private List<SKColor> ColorList = new List<SKColor>()
        {
            new SKColor(30,     30,     30),
            new SKColor(40,     40,     40 ),
            new SKColor(50,     50,     50 ),
            new SKColor(60,     60,     60 ),
            new SKColor(70,     70,     70 ),
            new SKColor(80,     80,     80 ),
            new SKColor(90,     90,     90 ),
            new SKColor(100,    100,    100),
            new SKColor(110,    110,    110),
            new SKColor(120,    120,    120),
            new SKColor(130,    130,    130),
            new SKColor(140,    140,    140),
            new SKColor(150,    150,    150),
            new SKColor(160,    160,    160),
            new SKColor(170,    170,    170),
            new SKColor(180,    180,    180),
            new SKColor(190,    190,    190),
            new SKColor(200,    200,    200),
            new SKColor(210,    210,    210),
            new SKColor(220,    220,    220),
            new SKColor(240,    240,    240),
        };
    }
}
